package com.clouddisk.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clouddisk.common.exception.BusinessException;
import com.clouddisk.common.util.JwtUtil;
import com.clouddisk.user.dto.UserDTO;
import com.clouddisk.user.dto.UserLoginDTO;
import com.clouddisk.user.dto.UserRegisterDTO;
import com.clouddisk.user.entity.User;
import com.clouddisk.user.mapper.UserMapper;
import com.clouddisk.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
    private final JwtUtil jwtUtil;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserDTO register(UserRegisterDTO registerDTO) {
        // 检查用户名是否已存在
        if (lambdaQuery().eq(User::getUsername, registerDTO.getUsername()).count() > 0) {
            throw new BusinessException("用户名已存在");
        }
        
        // 检查邮箱是否已存在
        if (lambdaQuery().eq(User::getEmail, registerDTO.getEmail()).count() > 0) {
            throw new BusinessException("邮箱已被使用");
        }
        
        // 创建用户
        User user = new User();
        user.setUsername(registerDTO.getUsername());
        user.setPassword(BCrypt.hashpw(registerDTO.getPassword()));
        user.setEmail(registerDTO.getEmail());
        user.setStorageQuota(5L * 1024 * 1024 * 1024); // 默认5GB
        user.setUsedStorage(0L);
        user.setStatus(1);
        
        // 保存用户
        save(user);
        
        // 转换为DTO返回
        return BeanUtil.copyProperties(user, UserDTO.class);
    }
    
    @Override
    public String login(UserLoginDTO loginDTO) {
        // 查询用户
        User user = lambdaQuery()
                .eq(User::getUsername, loginDTO.getUsername())
                .one();
        
        // 验证用户存在且密码正确
        if (user == null || !BCrypt.checkpw(loginDTO.getPassword(), user.getPassword())) {
            throw new BusinessException("用户名或密码错误");
        }
        
        // 检查用户状态
        if (user.getStatus() != 1) {
            throw new BusinessException("账号已被禁用");
        }
        
        // 生成JWT token
        return jwtUtil.createToken(user.getId());
    }
    
    @Override
    public UserDTO getUserInfo(Long id) {
        // 查询用户
        User user = getById(id);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        
        // 转换为DTO返回
        return BeanUtil.copyProperties(user, UserDTO.class);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserDTO updateUsedStorage(Long id, Long size) {
        // 查询用户
        User user = getById(id);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        
        // 计算新的已使用空间
        long newUsedStorage = user.getUsedStorage() + size;
        
        // 检查是否超出配额
        if (newUsedStorage > user.getStorageQuota()) {
            throw new BusinessException("存储空间不足");
        }
        
        // 更新已使用空间
        user.setUsedStorage(newUsedStorage);
        updateById(user);
        
        // 转换为DTO返回
        return BeanUtil.copyProperties(user, UserDTO.class);
    }
} 